﻿using Abp.Configuration;
using Abp.Dependency;
using Abp.Extensions;
using Mt.Site.Core.Configuration;
using System;
using System.Linq;
using System.Threading.Tasks;

namespace Mt.Site.Core.WorkFlows.Common
{
    public class OnWeeksChecker : IOnWeeksChecker, ITransientDependency
    {
        private readonly ISettingManager _settingManager;

        public OnWeeksChecker(ISettingManager settingManager)
        {
            _settingManager = settingManager;
        }

        public async Task<bool> CheckIsOnDudyDateAsync(DateTime date)
        {
            var scheduleSetting = await GetSettingInfoAsync();
            return CheckIsOnDudyDateInternal(date, scheduleSetting);
        }

        public bool CheckIsOnDudyDate(DateTime date)
        {
            var scheduleSetting = GetSettingInfo();
            return CheckIsOnDudyDateInternal(date, scheduleSetting);
        }


        public async Task<DateTime> CheckDaysToNowAsync(int dayCount)
        {
            var scheduleSetting = await GetSettingInfoAsync();

            return CheckDaysToNowInternal(dayCount, scheduleSetting);
        }

        public DateTime CheckDaysToNow(int dayCount)
        {
            var scheduleSetting = GetSettingInfo();
            return CheckDaysToNowInternal(dayCount, scheduleSetting);
        }

        public async Task<long> CheckDaysToNow_UnixAsync(int dayCount)
        {
            var date = await CheckDaysToNowAsync(dayCount);
            return date.ToUnixTimeByMilliseconds();
        }

        public long CheckDaysToNow_Unix(int dayCount)
        {
            var date = CheckDaysToNow(dayCount);
            return date.ToUnixTimeByMilliseconds();
        }

        /// <inheritdoc/>
        public async Task<DateTime> CheckDaysFromNowAsync(int dayCount)
        {
            var scheduleSetting = await GetSettingInfoAsync();
            return CheckDaysFromNowInternal(dayCount, scheduleSetting);
        }

        /// <inheritdoc/>
        public async Task<long> CheckDaysFromNow_UnixAsync(int dayCount)
        {
            var date = await CheckDaysFromNowAsync(dayCount);
            return date.ToUnixTimeByMilliseconds();
        }

        public DateTime CheckDaysFromNow(int dayCount)
        {
            var scheduleSetting = GetSettingInfo();
            return CheckDaysFromNowInternal(dayCount, scheduleSetting);
        }

        public long CheckDaysFromNow_Unix(int dayCount)
        {
            var date = CheckDaysFromNow(dayCount);
            return date.ToUnixTimeByMilliseconds();
        }



        public async Task<ScheduleSetting> GetSettingInfoAsync()
        {
            string JiaRi = await _settingManager.GetSettingValueAsync(MtAppSettings.TenantManagement.JiaRi);
            string TiaoXiu = await _settingManager.GetSettingValueAsync(MtAppSettings.TenantManagement.TiaoXiu);

            return new ScheduleSetting
            {
                OnDudyWeekend = TiaoXiu.GetArryFromStrBySplit<string>("|"),
                WeekDaysOff = JiaRi.GetArryFromStrBySplit<string>("|")
            };
        }
        public ScheduleSetting GetSettingInfo()
        {
            string JiaRi =  _settingManager.GetSettingValue(MtAppSettings.TenantManagement.JiaRi);
            string TiaoXiu = _settingManager.GetSettingValue(MtAppSettings.TenantManagement.TiaoXiu);

            return new ScheduleSetting
            {
                OnDudyWeekend = TiaoXiu.GetArryFromStrBySplit<string>("|"),
                WeekDaysOff = JiaRi.GetArryFromStrBySplit<string>("|")
            };
        }



        private bool CheckIsOnDudyDateInternal(DateTime date, ScheduleSetting scheduleSetting)
        {
            string dateStr = date.ToString("yyyy-MM-dd");

            if (date.DayOfWeek == DayOfWeek.Saturday || date.DayOfWeek == DayOfWeek.Sunday)
            {
                ///当日期是礼拜天或者礼拜六那就看是不是调休日期
                if (!scheduleSetting.OnDudyWeekend.Contains(dateStr))
                {
                    return false;
                }
            }
            else
            {
                ///当日期不是礼拜天或者礼拜六且那就看是不是放假日期
                if (scheduleSetting.WeekDaysOff.Contains(dateStr))
                {
                    return false;
                }
            }

            return true;
        }
        private DateTime CheckDaysToNowInternal(int dayCount, ScheduleSetting scheduleSetting)
        {
            DateTime date = DateTime.Now; //从今天往前计数   (一般下班时计算，今天算一天)

            int kk = 0;//过滤节假日，调休日
            int num = 0;//累计工作日天数

            DateTime? date1 = null;
            for (; num < dayCount; kk--)
            {
                date1 = date.AddDays(kk);
                if (CheckIsOnDudyDateInternal(date1.Value, scheduleSetting))
                {
                    num++;
                }
            }

            if (date1.HasValue)
            {
                var tempDate = date1.Value;
                return new DateTime(tempDate.Year, tempDate.Month, tempDate.Day,0,0,0,DateTimeKind.Local);
            }
            return date; //0天
        }

        private DateTime CheckDaysFromNowInternal(int dayCount, ScheduleSetting scheduleSetting)
        {
            DateTime date = DateTime.Now;//从今天往前计数   (一般早上上班时开始计算，今天算一天)

            int kk = 0;//过滤节假日，调休日
            int num = 0;//累计工作日天数

            DateTime? date1 = null;
            for (; num < dayCount; kk++)
            {
                date1 = date.AddDays(kk);
                if (CheckIsOnDudyDateInternal(date1.Value, scheduleSetting))
                {
                    num++;
                }
            }

            if (date1.HasValue)
            {
                var tempDate = date1.Value;
                return new DateTime(tempDate.Year, tempDate.Month, tempDate.Day, 0, 0, 0, DateTimeKind.Local);
            }
            return date; //0天
        }

    }
}
